//
//  JSLSelectBillProductController.m
//  JingleSpeedLoan
//
//  Created by 宋志彬 on 2018/8/5.
//  Copyright © 2018年 宋质彬. All rights reserved.
//

#import "JSLSelectBillProductController.h"
#import "JSLSelectBillProductCell.h"
#import "UIBarButtonItem+Item.h"
#import "JSLBillAddController.h"
#import "JSLBillEditController.h"
@interface JSLSelectBillProductController ()
<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger totalPage;
@property (nonatomic, strong) NSMutableArray *arrDataSource;

@end

@implementation JSLSelectBillProductController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"选择账单名称";
    [self baseConfig:PageGobackTypePop];
    
     self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithImage:nil highImage:nil title:@"增加账单名称" highTitle:nil font:LIGHT_FONT(13) textColor:[@"#333333" hexStringToColor] target:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self tableView];
    
   
    
  
    
}
- (void)viewWillAppear:(BOOL)animated {
      [self refreshData];
}

- (void)rightButtonClick:(UIButton *)sender {
    JSLBillAddController *addVC = [[JSLBillAddController alloc] init];
    
    [self pushViewController:addVC animated:YES];
}

#pragma mark - 请求数据
- (void)refreshData {
    
    NSArray * arr = [CacheManager getObject:@"JSL_BILL_PRODUCT"];
    self.arrDataSource = [NSMutableArray arrayWithArray:arr];
    [self.tableView reloadData];
 
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
    });
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrDataSource.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JSLSelectBillProductCell *cell = [JSLSelectBillProductCell cellWithTableView:tableView];
    cell.info = self.arrDataSource[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    JSLBillEditController *vc = [[JSLBillEditController alloc] init];
    vc.info = self.arrDataSource[indexPath.row];
    [self pushViewController:vc animated:YES];

    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"点击了删除");
        [self.arrDataSource removeObjectAtIndex:indexPath.row];
         [CacheManager setObject:self.arrDataSource forKey:@"JSL_BILL_PRODUCT"];
        [self.tableView reloadData];
        
    }];
    UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"编辑" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"点击了编辑");
        
        
        JSLBillAddController *addVC = [[JSLBillAddController alloc] init];
        addVC.info = self.arrDataSource[indexPath.row];
        addVC.row = indexPath.row;
        [self pushViewController:addVC animated:YES];
    }];
    editAction.backgroundColor = [UIColor grayColor];
    return @[deleteAction, editAction];
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    editingStyle = UITableViewCellEditingStyleDelete;
}
                    
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
//    return [JSLKitFactory jsl_viewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10*AutoSizeScaleYY) backgroundColor:BACKGROUND_COLOR superView:nil];
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
//    return 10 *AutoSizeScaleYY;
//}

#pragma mark - get
- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height-64 -49 ) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _tableView.scrollEnabled = YES;
        _tableView.rowHeight = 70 *AutoSizeScaleYY;
        _tableView.backgroundColor = BACKGROUND_COLOR;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_tableView];
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
        
        //        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
        
//        _tableView.tableHeaderView = [JSLKitFactory jsl_viewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10*AutoSizeScaleYY) backgroundColor:BACKGROUND_COLOR superView:nil];
        
    }
    
    return _tableView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
